<?php

ini_set("display_errors", "On");
error_reporting(E_ALL);

// 引入数据库连接文件
require_once '../../../sql/connection.php';

$courseID = $_POST['upCourseID'];
$jobID = $_POST['upJobID'];
$jobName = $_POST['upJobName'];

// 模板文件
if (!empty($_FILES)) {
	$tmpName = $_FILES['templateFile']['tmp_name'];     // 临时文件名称
	// $suffix = explode(".", $_FILES['templateFile']['name']);;         // 文件的后缀
	// 文件名
	$fileName = $_FILES['templateFile']['name'];
	// 上传目录
	$path = '/upload/teacher/' . $courseID . '/' . $jobID . '/';

	// 判断目录是否存在，如果不存在则新建目录
	if (!is_dir('../../..'.$path)) {
		// 创建目录
		mkdir('../../..'.$path, 0777, true);
	}
	
	// $file_name = date('YmdHis').rand(100,999).$name;// 避免文件重名，更改文件名称
	if (move_uploaded_file($tmpName, '../../..'.$path . $fileName)) {
		$now = date('Y-m-d H:i:s', time());
		$templateFile = $path.$fileName;
		$uploadSql = "UPDATE `job_info` SET template_file = '$templateFile' , question_total = 1 WHERE id = '$jobID'";
		
		if (mysqli_query($GLOBALS['conn'], $uploadSql)) {
			echo "
            <script>
                alert('模板上传成功！');
                window.location.href=document.referrer;
            </script>
        ";
		} else {
			echo "
            <script>
                alert('模板存储失败！');
                history.back();
            </script>
        ";
		}
	}
} else {
	echo "
            <script>
                alert('文件上传失败！');
                history.back();
            </script>
        ";
	
}

	




